package alo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;

/**
 * @ClassName t6
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/4/20 11:18
 * @Version 1.0
 */
public class t6 {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> list=new ArrayList<>();
        if(k>input.length||k<=0)return list;
        PriorityQueue<Integer> queue=new PriorityQueue<>((x,y)->y-x);
        for (int i = 0; i < k; i++) {
            queue.offer(input[i]);
        }
        for (int i = k; i < input.length; i++) {
            if (queue.peek()>input[i]){
                queue.poll();
                queue.offer(input[i]);
            }
        }
        for (Integer i :
                queue) {
            list.add(queue.poll());
        }
        return list;
    }
}
